package no.hib.quiz;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class TakeQuizFragment extends Fragment{
	
	private View aView;
	private Quiz activeQuiz;
	private int progressCounter;
	private int score;

	public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
	
	}
	
	
	public void quizMethod() {
		TextView tv = (TextView) aView.findViewById(R.id.tv_question_asked);
		String q = activeQuiz.getQandAArr()[progressCounter].getQuestion();
		tv.setText(q);
		Button btnOk =(Button)aView.findViewById(R.id.btn_ok);
		btnOk.setOnClickListener(new OnClickListener() {

	        @Override
	        public void onClick(View v) {
	        	TextView tv = (TextView) aView.findViewById(R.id.tv_question_asked);
	    		EditText et = (EditText) aView.findViewById(R.id.et_question_anwered);
	        	
	        	if (progressCounter < 9) {
	        		
		    		
		    		String answer = et.getText().toString();
		        	if (answer.trim().length() > 0) {
		        		int answerInt = Integer.parseInt(answer);
		        		if (answerInt == activeQuiz.getQandAArr()[progressCounter].getAnswer()) {
		        			score++;
		        		}
		        		progressCounter++;
		        		tv.setText(activeQuiz.getQandAArr()[progressCounter].getQuestion());
		        		et.setText("");
		        		
		        		
		        		
		        	}
		        	else {
	        			et.setHint("Invalid answer");
	        		}
	        	}
	        	else {
	        		
	        		String answer = et.getText().toString();
	        		if (answer.trim().length() > 0) {
		        		int answerInt = Integer.parseInt(answer);
		        		if (answerInt == activeQuiz.getQandAArr()[progressCounter].getAnswer()) {
		        			score++;
		        		}
		        		progressCounter = 0;
		        		
		        		new AlertDialog.Builder(getActivity())
		        		.setTitle("Score")
		        		.setMessage("Your score is " + score)
		        		.setPositiveButton("OK", null)
		        		.show();
		        		
		        		FragmentManager fragmentManager = getFragmentManager();
		                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

		                MenuFragment newMenu = new MenuFragment();
		                fragmentTransaction.replace(R.id.fragment_container, newMenu);
		                fragmentTransaction.addToBackStack(null);
		                fragmentTransaction.commit();
		   
		        	}
		        	else {
	        			et.setHint("Invalid answer");
	        		}
	        		
	        		
	        		
	        	}
	
	        }

			
	    });
	}
	
	
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
		
		aView = inflater.inflate(R.layout.take_quiz, container, false);
		activeQuiz = MainActivity.quizList.get(MainActivity.quizNumber);
		progressCounter = 0;
		score = 0;
		
	
		quizMethod();
    	return aView;
    	
    	
		
	}
	
	

}
